package com.atguigu.controller;

import com.atguigu.mapper.EmployeeMapper;
import com.atguigu.pojo.Employee;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;
import java.util.Map;

@Controller
public class EmployeeLogin {
    @Autowired
    private EmployeeMapper employeeMapper;
    @PostMapping("/user/login")
    public String emptLogin(@RequestParam("username") String username,
                            @RequestParam("password") String password,
                            HttpSession session, Model model
                            ){
        List<Employee> allEmpts = employeeMapper.getAllEmpts();
        if(allEmpts==null|| allEmpts.isEmpty()){
            model.addAttribute("msg","用户不存在");
            return "index";
        }
        for (Employee empt : allEmpts) {
            if(empt.getName().equals(username)&&empt.getPassword().equals(password)){
                session.setAttribute("loginEmpt",username);
                return "redirect:/main.html";
            }
        }
        model.addAttribute("msg","用户名或者密码错误");
        return "index";
    }
}
